package com.henu.yizhiya.util;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.henu.R;
import com.henu.yizhiya.entity.Course;
import com.henu.yizhiya.entity.Room;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.bmob.v3.Bmob;
import cn.bmob.v3.BmobBatch;
import cn.bmob.v3.BmobObject;
import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.datatype.BatchResult;
import cn.bmob.v3.datatype.BmobQueryResult;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;
import cn.bmob.v3.listener.QueryListListener;
import cn.bmob.v3.listener.SQLQueryListener;

public class CourseToRoomActivity extends AppCompatActivity {
    private Button btn_c_r;
    private List<Course> course_list;
    private List<Room> room_list;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_course_to_room);
        //引入Bmob
        Bmob.initialize(this, "a24db0fac16b375a97072b85080ea20f");
        //
        btn_c_r = (Button) findViewById(R.id.btn_c_r);
        //由课程表转换成自习室表

        btn_c_r.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                BmobQuery<Course> query = new BmobQuery<>();
                query.setLimit(1000);
                query.findObjects(new FindListener<Course>() {
                    @Override
                    public void done(List<Course> list, BmobException e) {
                        if (e == null) {
                            course_list = list;
                            if (course_list != null) {

                                room_list = onSwitch(course_list);
                                System.out.println("长度---"+room_list.size());
                                for (Room r : room_list) {
                                    System.out.println("week_1-" + r.getWeek_1() + "-week_2-" + r.getWeek_2()
                                            + "-week_3-" + r.getWeek_3() + "-week_4" + r.getWeek_4()
                                            + "-week_5--" + r.getWeek_5() + "-week_6-" + r.getWeek_6()
                                            + "-week_7--" + r.getWeek_7() + "-r_num" + r.getR_num());
                                }
                                saveRoom(room_list);
                                Toast.makeText(CourseToRoomActivity.this, "转存成功", Toast.LENGTH_SHORT).show();
                            } else {
                                Toast.makeText(CourseToRoomActivity.this, "获取的课程为空", Toast.LENGTH_SHORT).show();
                            }
                        } else {
                            Toast.makeText(CourseToRoomActivity.this, "获取课程数据失败" + e.getMessage(), Toast.LENGTH_SHORT).show();
                        }
                    }
                });

            }
        });
    }
    //保存房间表
    public void saveRoom(List<Room> rooms){
        List<BmobObject> list = new ArrayList<>();
        for (Room room:rooms) {
            list.add(room);
        }
        new BmobBatch().insertBatch(list).doBatch(new QueryListListener<BatchResult>() {
            @Override
            public void done(List<BatchResult> list, BmobException e) {
                if(e==null){
                    for(int i=0;i<list.size();i++){
                        BatchResult result = list.get(i);
                        BmobException ex =result.getError();
                        if(ex==null){
                            System.out.println("第"+i+"个数据批量添加成功："+result.getCreatedAt());
                        }else{
                            System.out.println("第"+i+"个数据批量添加失败："+ex.getMessage()+","+ex.getErrorCode());
                        }
                    }
                }else{
                    Log.i("bmob","失败："+e.getMessage()+","+e.getErrorCode());
                }
            }
        });
    }

    //将课程表转换为自习室表
    public List<Room> onSwitch(List<Course> courses) {
        Map<String, List<Course>> map = new HashMap<>();
        List<Room> roomList = new ArrayList<>();
        List<Course> list;
        for (Course c : courses) {
            if (map.containsKey(c.getR_num())) {
                list = map.get(c.getR_num());
                list.add(c);
            } else {
                list = new ArrayList<>();
                list.add(c);
                map.put(c.getR_num(), list);
            }
        }
        for (Map.Entry<String, List<Course>> item : map.entrySet()) {
            Room room = onChange(item.getValue());
            roomList.add(room);
        }

        return roomList;
    }


    //针对一个房间进行转换
    public Room onChange(List<Course> list) {
        Room room = new Room();
        for (Course c : list) {
            room.setR_num(c.getR_num());
            String binaryResult = switchToBinary(c);
            switch (c.getWeek()) {
                case "week_1":
                    room.setWeek_1(binaryResult);
                    break;
                case "week_2":
                    room.setWeek_2(binaryResult);
                    break;
                case "week_3":
                    room.setWeek_3(binaryResult);
                    break;
                case "week_4":
                    room.setWeek_4(binaryResult);
                    break;
                case "week_5":
                    room.setWeek_5(binaryResult);
                    break;
                case "week_6":
                    room.setWeek_6(binaryResult);
                    break;
                case "week_7":
                    room.setWeek_7(binaryResult);
                    break;
            }

        }
        return room;
    }

    public String switchToBinary(Course c) {
        StringBuffer result = new StringBuffer();
        if (c.getPeriod_1() == "") {
            result.append("0");
        } else {
            result.append("1");
        }
        if (c.getPeriod_2() == "") {
            result.append("0");
        } else {
            result.append("1");
        }
        if (c.getPeriod_3() == "") {
            result.append("0");
        } else {
            result.append("1");
        }
        if (c.getPeriod_4() == "") {
            result.append("0");
        } else {
            result.append("1");
        }
        if (c.getPeriod_5() == "") {
            result.append("0");
        } else {
            result.append("1");
        }
        if (c.getPeriod_6() == "") {
            result.append("0");
        } else {
            result.append("1");
        }
        return result.toString();
    }


}
